package com.controller;

import com.dao.TeacherDao;
import com.dto.DTO;
import com.dto.MessageEnum;
import com.entity.Teacher;
import com.page.PageInfo;
import com.propbean.UpLoadPropBean;
import com.service.TeacherService;
import com.utils.GenNumTools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.rmi.CORBA.Util;
import java.util.List;
import java.util.Optional;

@CrossOrigin
@Controller //@Controller注解是用spring容器生成 对象 默认对象名 teacherController
@RequestMapping("/teacherController") //@RequestMapping 用户请求查找该资源使用的名字 网名 默认  /teacherController
public class TeacherController {


	@Resource
	TeacherDao teacherDao;

	@Resource
	TeacherService teacherService;
//	@RequestMapping("/getAllTeacher")
//	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
//    public DTO<Teacher> getAllTeacher(){
//    	System.out.println("查询全部教师");
//    	List<Teacher> teachers =  teacherDao.getAllTeachers();
//    	return new DTO<Teacher>(200, "查询全部教师",teachers);
//    }

	@RequestMapping("/deleteTeacherById/{aId}")
	@ResponseBody
    public DTO<Teacher> deleteTeacherById(@PathVariable("aId") String aId)throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(aId+"删除教师");
    	return new DTO<Teacher>(200, "删除"+aId+"教师成功");
    }


    @RequiresPermissions("teacher:getByPage")
	@RequestMapping("/getTeacherByPage")
	@ResponseBody
	public DTO<Teacher> getTeacherByPage(PageInfo pageInfo)throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		String cacheId = "TeacherByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
		System.out.println(cacheId);
		return pageInfo.getCurrPage() > 2 ? teacherService.getTeachersByPage(pageInfo) :
				teacherService.getTeachersByPage(pageInfo, cacheId);
	}

	@RequestMapping("/selectByTnoWithScs/{tno}")
	@ResponseBody
	public DTO<Teacher> selectByTnoWithScs(@PathVariable("tno")String tno)throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询教师授课");
		Teacher teacher = teacherDao.selectByTnoWithScs(tno);
		return new DTO<Teacher>(200, "分页查询教师成功",null,teacher);
	}



	@PostMapping("/addTeacher")
	@ResponseBody
	public DTO<Teacher> addTeacher(@RequestBody Teacher teacher)throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加教师授课");
		teacher.setTno(GenNumTools.initId("",4,teacherDao.getMaxId()));
		Integer result = teacherDao.addTeacher(teacher);
		DTO<Teacher> dto = result > 0 ? new DTO(MessageEnum.GETSTUDENTSUCCESS.getCode(),MessageEnum.GETSTUDENTSUCCESS.getMessage()):
				new DTO(MessageEnum.GETSTUDENTFAIL.getCode(),MessageEnum.GETSTUDENTFAIL.getMessage());
		return dto;
	}

	@PostMapping("/editTeacher")
	@ResponseBody
	public DTO<Teacher> editTeacher(@RequestBody Teacher teacher)throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
       return teacherService.editTeacher(teacher);
	}


}
